﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.Revit
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public abstract class HYNoTranExternalCommand : IExternalCommand
	{
		public Document Doc
		{
			get
			{
				return this.doc;
			}
		}

		public Selection Sel
		{
			get
			{
				return this.sel;
			}
		}

		public UIApplication UiApp
		{
			get
			{
				return this.uiApp;
			}
		}

		public Application App
		{
			get
			{
				return this.app;
			}
		}

		public UIDocument UiDoc
		{
			get
			{
				return this.uiDoc;
			}
		}

		public List<ViewType> FilterViewType
		{
			get
			{
				return this.filterViewType;
			}
		}

		public abstract Result HYExecute();

		public virtual void FilterProcess()
		{
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.commandData = commandData;
			this.elements = elements;
			this.uiApp = commandData.Application;
			this.app = this.UiApp.Application;
			this.uiDoc = this.UiApp.ActiveUIDocument;
			this.doc = this.UiDoc.Document;
			this.sel = this.UiDoc.Selection;
			this.filterViewType = new List<ViewType>();
			this.filterViewType.Add(1);
			this.filterViewType.Add(2);
			this.FilterProcess();
			if (this.filterViewType.Count > 0)
			{
				bool flag = false;
				foreach (ViewType viewType in this.filterViewType)
				{
					if (this.doc.ActiveView.ViewType == viewType)
					{
						flag = true;
					}
				}
				if (!flag)
				{
					List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(this.doc, BuiltInCategory.OST_Views);
					List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
					foreach (Element element in elementByCategory)
					{
						foreach (ViewType viewType2 in this.filterViewType)
						{
							if ((element as Autodesk.Revit.DB.View).ViewType == viewType2)
							{
								list.Add(element as Autodesk.Revit.DB.View);
								break;
							}
						}
					}
					ViewFormSelect viewFormSelect = new ViewFormSelect(list);
					if (viewFormSelect.ShowDialog() != DialogResult.OK || !(viewFormSelect.viewId != null))
					{
						return Autodesk.Revit.UI.Result.Cancelled;
					}
					this.uiDoc.ActiveView = (this.doc.GetElementById(viewFormSelect.viewId) as Autodesk.Revit.DB.View);
				}
			}
			Result result = -1;
			try
			{
				result = this.HYExecute();
				message = this.message;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				this.TailProcess();
			}
			return result;
		}

		public virtual void TailProcess()
		{
		}

		public ExternalCommandData commandData;

		public string message = "";

		public ElementSet elements;

		private UIApplication uiApp;

		private Application app;

		private UIDocument uiDoc;

		private Document doc;

		private Selection sel;

		private List<ViewType> filterViewType;
	}
}
